﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class IntransitBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IIntransitDA IntransitDA = DataAccess.CreateIntransitDA();
        private static readonly IIntransitDetailDA IntransitDetailDA = DataAccess.CreateIntransitDetailDA();
        private PubParasInfo pubParasInfo;

        public IntransitBLL()
        {
            this.pubParasInfo = null;
        }

        public IntransitBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public IntransitBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteIntransitInfo(List<string> IntransitIDs)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<IntransitInfo> intransitInfos = new List<IntransitInfo>();
                IntransitInfo intransitInfo = null;
                foreach (string id in IntransitIDs)
                {
                    intransitInfo = IntransitDA.GetIntransitByID(dataBase, tran, id, this.dataBaseID[0]);
                    intransitInfo.IntransitDetailInfos = IntransitDetailDA.GetIntransitDetailListByID(dataBase, tran, id, this.dataBaseID[0]);
                    intransitInfos.Add(intransitInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                IntransitDetailDA.DeleteIntransitDetail(dataBase, tran, IntransitIDs, this.dataBaseID);
                IntransitDA.DeleteIntransit(dataBase, tran, IntransitIDs, this.dataBaseID);
                for (int i = 0; i < IntransitIDs.Count; i++)
                {
                    auditDataBLL.InsertAuditData<IntransitInfo>(dataBase, tran, intransitInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "INTRANSIT", this.dataBaseID[0]);
                    if ((intransitInfos[i].IntransitDetailInfos != null) && (intransitInfos[i].IntransitDetailInfos.Count > 0))
                    {
                        for (int j = 0; j < intransitInfos[i].IntransitDetailInfos.Count; j++)
                        {
                            auditDataBLL.InsertAuditData<IntransitDetailInfo>(dataBase, tran, intransitInfos[i].IntransitDetailInfos[j], null, AuditAction.Delete, this.pubParasInfo.UserID, "INTRANSIT_DETAIL", this.dataBaseID[0]);
                        }
                    }
                    temp = temp + IntransitIDs[i] + ",";
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllIntransit()
        {
            return IntransitDA.GetAllIntransit(this.dataBaseID);
        }

        public IntransitInfo GetIntransitInfoByID(string IntransitID, string srcDbID)
        {
            IntransitInfo intransitInfo = IntransitDA.GetIntransitByID(IntransitID, srcDbID, this.dataBaseID);
            if (intransitInfo != null)
            {
                List<string> tempDataBaseID = commonMethod.GetDataBaseID(srcDbID);
                intransitInfo.IntransitDetailInfos = IntransitDetailDA.GetIntransitDetailListByID(IntransitID, tempDataBaseID[0]);
            }
            return intransitInfo;
        }

        public IntransitInfo GetIntransitInfoByID(DataBase dataBase, DbTransaction tran, string IntransitID, string srcDbID)
        {
            string whLoginID = commonMethod.GetDataBaseID(dataBase, tran, srcDbID)[0];
            IntransitInfo intransitInfo = IntransitDA.GetIntransitByID(dataBase, tran, IntransitID, whLoginID);
            if (intransitInfo != null)
            {
                intransitInfo.IntransitDetailInfos = IntransitDetailDA.GetIntransitDetailListByID(dataBase, tran, IntransitID, whLoginID);
            }
            return intransitInfo;
        }

        public DataSet GetIntransitInfoByQueryEntity(IntransitQueryEntity intransitQuery)
        {
            IntransitQueryEntity IntransitQuery = intransitQuery;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(IntransitQuery.IntransitID))
            {
                sqlWhere.Add(" INTRANSIT_ID " + IntransitQuery.IntransitID);
            }
            if (!string.IsNullOrEmpty(IntransitQuery.SrcWhDbID))
            {
                sqlWhere.Add(" SRC_WH_DB_ID " + IntransitQuery.SrcWhDbID);
            }
            if (!string.IsNullOrEmpty(IntransitQuery.DestWhDbID))
            {
                sqlWhere.Add(" DEST_WH_DB_ID " + IntransitQuery.DestWhDbID);
            }
            if (!string.IsNullOrEmpty(IntransitQuery.OwnerID))
            {
                sqlWhere.Add(" OWNER_ID " + IntransitQuery.OwnerID);
            }
            if (!string.IsNullOrEmpty(IntransitQuery.OrderID))
            {
                sqlWhere.Add(" ORDER_ID " + IntransitQuery.OrderID);
            }
            if (!string.IsNullOrEmpty(IntransitQuery.ReceiptID))
            {
                sqlWhere.Add(" RECEIPT_ID " + IntransitQuery.ReceiptID);
            }
            if (!string.IsNullOrEmpty(IntransitQuery.OrderDate))
            {
                sqlWhere.Add(" ORDER_DATE " + IntransitQuery.OrderDate);
            }
            if (!string.IsNullOrEmpty(IntransitQuery.Status))
            {
                sqlWhere.Add(" STATUS " + IntransitQuery.Status);
            }
            if (!string.IsNullOrEmpty(IntransitQuery.IsActive))
            {
                sqlWhere.Add(" temp.IS_ACTIVE " + IntransitQuery.IsActive);
            }
            return IntransitDA.GetIntransitByQueryList(sqlWhere, IntransitQuery, this.dataBaseID);
        }

        public string GetIntransitInfoStatus(IntransitInfo intransitInfo)
        {
            string status = IntransitStatus.New;
            if (((intransitInfo != null) && (intransitInfo.IntransitDetailInfos != null)) && (intransitInfo.IntransitDetailInfos.Count > 0))
            {
                decimal qty = 0M;
                decimal shippedQty = 0M;
                decimal receivedQty = 0M;
                foreach (IntransitDetailInfo intransitDetailInfo in intransitInfo.IntransitDetailInfos)
                {
                    qty += intransitDetailInfo.Qty;
                    shippedQty += intransitDetailInfo.ShippedQty;
                    receivedQty += intransitDetailInfo.ReceivedQty;
                }
                if (shippedQty == 0M)
                {
                    status = IntransitStatus.New;
                }
                if ((shippedQty > 0M) && (shippedQty <= qty))
                {
                    status = IntransitStatus.InTransit;
                }
                if ((receivedQty > 0M) && (receivedQty < qty))
                {
                    status = IntransitStatus.InTransit;
                }
                if (receivedQty >= qty)
                {
                    status = IntransitStatus.Completed;
                }
            }
            return status;
        }

        public string InsertIntransitInfo(IntransitInfo intransitInfo)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string IntransitID = string.Empty;
            try
            {
                if (!string.IsNullOrEmpty(intransitInfo.IntransitID))
                {
                    if (IntransitDA.CheckIntransitIDUnique(dataBase, tran, intransitInfo.IntransitID, this.dataBaseID[0]))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F13500_001", this.pubParasInfo.UserLanguage, new List<string> { intransitInfo.IntransitID });
                    }
                    else
                    {
                        IntransitID = intransitInfo.IntransitID;
                    }
                }
                else
                {
                    intransitInfo.IntransitID = commonMethod.GenNextNumStr(dataBase, tran, "INTRANSIT", this.dataBaseID[0]);
                }
                intransitInfo.UpdatedBy = intransitInfo.CreatedBy;
                intransitInfo.CreatedDate = DateTime.Now;
                intransitInfo.UpdatedDate = DateTime.Now;
                IntransitDA.InsertIntransit(dataBase, tran, intransitInfo, this.dataBaseID);
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                auditDataBLL.InsertAuditData<IntransitInfo>(dataBase, tran, null, intransitInfo, AuditAction.Insert, this.pubParasInfo.UserID, "INTRANSIT", this.dataBaseID[0]);
                IntransitID = intransitInfo.IntransitID;
                if ((intransitInfo.IntransitDetailInfos != null) && (intransitInfo.IntransitDetailInfos.Count > 0))
                {
                    for (int i = 0; i < intransitInfo.IntransitDetailInfos.Count; i++)
                    {
                        intransitInfo.IntransitDetailInfos[i].IntransitID = IntransitID;
                        if (IntransitDetailDA.CheckIntransitDetailIDUnique(dataBase, tran, intransitInfo.IntransitDetailInfos[i].IntransitID, intransitInfo.IntransitDetailInfos[i].LineID.ToString(), this.dataBaseID[0]))
                        {
                            PublicMethod.GetInstance().ShowAlertMessage("F13500_002", this.pubParasInfo.UserLanguage, new List<string> { intransitInfo.IntransitDetailInfos[i].IntransitID, intransitInfo.IntransitDetailInfos[i].LineID.ToString() });
                        }
                        else
                        {
                            intransitInfo.IntransitDetailInfos[i].CreatedDate = DateTime.Now;
                            intransitInfo.IntransitDetailInfos[i].UpdatedDate = DateTime.Now;
                            intransitInfo.IntransitDetailInfos[i].CreatedBy = this.pubParasInfo.UserID;
                            intransitInfo.IntransitDetailInfos[i].UpdatedBy = this.pubParasInfo.UserID;
                            IntransitDetailDA.InsertIntransitDetail(dataBase, tran, intransitInfo.IntransitDetailInfos[i], this.dataBaseID);
                            auditDataBLL.InsertAuditData<IntransitDetailInfo>(dataBase, tran, null, intransitInfo.IntransitDetailInfos[i], AuditAction.Insert, this.pubParasInfo.UserID, "INTRANSIT_DETAIL", this.dataBaseID[0]);
                        }
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return IntransitID;
        }

        public string UpdateIntransitInfo(IntransitInfo intransitInfo)
        {
            string IntransitID = string.Empty;
            try
            {
                IntransitInfo oldIntransitInfo = IntransitDA.GetIntransitByID(intransitInfo.IntransitID, this.dataBaseID[0]);
                intransitInfo.UpdatedBy = this.pubParasInfo.UserID;
                intransitInfo.UpdatedDate = DateTime.Now;
                if (IntransitDA.UpdateIntransit(intransitInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<IntransitInfo>(oldIntransitInfo, intransitInfo, AuditAction.Update, this.pubParasInfo.UserID, "INTRANSIT_DETAIL", this.dataBaseID[0]);
                    IntransitID = intransitInfo.IntransitID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return IntransitID;
        }
    }
}

